// TitleView.cpp: implementation of the CTitleView class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Model.h"
#include "Rock.h"
#include "TitleView.h"
#include "MainToolbar.h"
#include "SystemSet.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTitleView::CTitleView()
{	
    m_TitleHeight  = 18;
    m_NcBoderWidth = 0;

	m_crViewBkg = SS.crMainTitleViewBk;
    m_crWinBorder = SS.crMainTitleViewBorder; 

    CloseboxItem* Closebox = new CloseboxItem(ID_CLOSE,
		SS.crMainTitleBntBorder,
		SS.crMainTitleBntNormal,
		SS.crMainTitleBntFocuse,
		SS.crMainTitleBntSelected);
	TextItem*     TextLabel= new TextItem(0,
		SS.crMainTitleBntNormal,
		SS.crMainTitleBntFocuse);
   
	NcBorderItem* TitleSpace = m_NcBorderList[0];
	TitleSpace->SetColor(m_crViewBkg,m_crViewBkg,m_crViewBkg);
	
	TitleSpace->Push(TextLabel);
	TitleSpace->Push(Closebox);
}

CTitleView::~CTitleView()
{

}
    
void CTitleView::SetID(int64 ID){
	m_ID = ID;
	NcBorderItem* TitleSpace = m_NcBorderList[0];
    CloseboxItem* Closebox = (CloseboxItem*)TitleSpace->m_ChildList[1];
    Closebox->m_ID = ID;
}

void CTitleView::SetTitle(const char* Text){
	NcBorderItem* TitleSpace = m_NcBorderList[0];
	TextItem*     TextLabel = (TextItem*)TitleSpace->m_ChildList[0];
	TextLabel->m_Text = Text;
	if(GetHwnd())Invalidate();
}

void CTitleView::NcLayout(){
	
	CWSTitleView::NcLayout();
	
	NcBorderItem* TitleSpace = m_NcBorderList[0];
    if(TitleSpace->m_ChildList.size()==0)return;

	TextItem*     TextLabel = (TextItem*)TitleSpace->m_ChildList[0];
    CloseboxItem* Closebox  = (CloseboxItem*)TitleSpace->m_ChildList[1];
	
	Closebox->m_Area = TitleSpace->m_Area;
	int32 len = (RectHeight(TitleSpace->m_Area)-13)/2;
	DeflateRect(Closebox->m_Area,len,len,len,len);	
	
	Closebox->m_Area.left = Closebox->m_Area.right-14;
    
	TextLabel->m_Area = TitleSpace->m_Area;
	TextLabel->m_Area.left +=4;
	TextLabel->m_Area.right = Closebox->m_Area.left-4;
}
LRESULT CTitleView::Reaction(UINT message, WPARAM wParam, LPARAM lParam){

	switch(message){

	case WM_LBUTTONDOWN:
		return OnLButtonDown(wParam,lParam);
	default:
		return CWSTitleView::Reaction(message,wParam,lParam);
	}
}
LRESULT CTitleView::OnLButtonDown(WPARAM wParam, LPARAM lParam) 
{
    POINTS* p = (POINTS*)(&lParam);
	POINT point;
	point.x = p->x;
	point.y = p->y;

    CSpace2 * Space = NcHit(point);	
	if(Space){
		int64 ID = Space->m_ID;
        if(ID==ID_CLOSE){
			SendParentRectionMessage(WM_LBUTTONDOWN,wParam,lParam,Space);
		}
	}
    return 0;
}

CThreadTitleView::CThreadTitleView()
{	
    m_TitleHeight  = 18;
    m_NcBoderWidth = 0;

	m_crViewBkg   = SS.crThreadTitleViewBk;
    m_crWinBorder = SS.crThreadTitleViewBorder; 

	NcBorderItem* TitleSpace = m_NcBorderList[0];
	TitleSpace->SetColor(m_crViewBkg,m_crViewBkg,m_crViewBkg);
	
}